package com.pb.wkflow.core.listener;

import com.pb.wkflow.core.camunda.bpmn.instance.ExternalInterface;
import com.pb.wkflow.core.camunda.bpmn.instance.Priority;
import com.pb.wkflow.core.camunda.bpmn.instance.UserParticipant;
import com.pb.wkflow.core.camunda.bpmn.instanceImpl.*;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.impl.bpmn.parser.BpmnParseListener;
import org.camunda.bpm.engine.impl.cfg.AbstractProcessEnginePlugin;
import org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.camunda.bpm.model.bpmn.Bpmn;
import org.camunda.bpm.model.xml.ModelBuilder;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 *  org.camunda.bpm.spring.boot.starter.CamundaBpmConfiguration.processEngineConfigurationImpl
 * 流程全局事件注入插件
 * @author 苗益辉
 * @date 2022/4/26 17:00
 */
@Slf4j
@Component
public class CamundaGlobalListenerPlugin extends AbstractProcessEnginePlugin implements InitializingBean {

    private final CamundaGlobalListener camundaGlobalListener = new CamundaGlobalListener();

    @Override
    public void preInit(ProcessEngineConfigurationImpl processEngineConfiguration) {
        log.info("加载全局自定义监听插件 CamundaGlobalListenerPlugin");
        List<BpmnParseListener> preParseListeners = processEngineConfiguration.getCustomPreBPMNParseListeners();
        if(preParseListeners == null) {
            preParseListeners = new ArrayList<>();
        }
        preParseListeners.add(camundaGlobalListener);
        processEngineConfiguration.setCustomPreBPMNParseListeners(preParseListeners);
    }

    @Override
    public void afterPropertiesSet() {
        log.info("加载自定义元素");
        ModelBuilder bpmnModelBuilder = Bpmn.INSTANCE.getBpmnModelBuilder();
        //参与者类型
        UserParticipantImpl.registerType(bpmnModelBuilder);
        //参与者
        ParticipantsImpl.registerType(bpmnModelBuilder);
        //规则逻辑
        RuleLogicParamImpl.registerType(bpmnModelBuilder);
        //会签
        VoteWorkImpl.registerType(bpmnModelBuilder);
        //服务远程接口
        ExternalInterfaceImpl.registerType(bpmnModelBuilder);
        //连接线优先级
        PriorityImpl.registerType(bpmnModelBuilder);
    }
}
